package main

import (
	"fmt"
	"io/ioutil"
	"net/http"
	"os"
	"strings"
	"time"
)

const MSG = "网络监控程序，方便告警，请不要关闭本窗口"
const SERVER = "http://localhost:6300/api/heartbeat?mac=%s"

//GetMAC 获取唯一标示符
func GetMAC() (mac string) {
	inputFile := ""
	if _, err := os.Stat("id.txt"); err == nil {
		// path/to/whatever exists
		inputFile = "id.txt"
	} else if _, err := os.Stat("c:\\id.txt"); err == nil {
		inputFile = "c:\\id.txtid.txt"
	} else {
		fmt.Println("id.txt不存在")
		return
	}

	buf, _ := ioutil.ReadFile(inputFile)

	fileContent := strings.TrimSpace(string(buf))
	if fileContent == "" {
		fmt.Println("id.txt 内容不存在，请填写 该台机器链接")
		return
	}
	mac = fileContent
	return
}

func Client() {
	fmt.Println(MSG)
	mac := GetMAC()
	if mac == "" {
		time.Sleep(3 * time.Second)
		return
	}
	go func() {
		for range time.Tick(time.Second * 3) {
			http.Get(fmt.Sprintf(SERVER, mac))
		}
	}()
	http.HandleFunc("/sdsadsadsadsa", func(w http.ResponseWriter, r *http.Request) {
		fmt.Fprintf(w, "Hello")
	})
	http.ListenAndServe("localhost:12889", nil)
}

func main() {
	Client()
}
